package com.jinxin.platform.cdsideparkmgr.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdsideparkmgr.bean.CdparkmanagerProject;
import com.jinxin.platform.cdsideparkmgr.bean.CdparkmanagerProjectCamera;
import com.jinxin.platform.cdsideparkmgr.bean.CdyardmgrDeviceOnline;
import com.jinxin.platform.cdsideparkmgr.bean.bo.PushInfo;
import com.jinxin.platform.cdsideparkmgr.bean.bo.ResponseData;
import com.jinxin.platform.cdsideparkmgr.bean.enums.ResultType;
import com.jinxin.platform.cdsideparkmgr.common.Constant;
import com.jinxin.platform.cdsideparkmgr.mapper.CdsideparkmgrDeviceOnlineMapper;
import com.jinxin.platform.cdsideparkmgr.mapper.CdsideparkmgrProjectCameraMapper;
import com.jinxin.platform.cdsideparkmgr.mapper.CdsideparkmgrProjectMapper;
import com.jinxin.platform.cdsideparkmgr.service.ICdsideparkmageDeviceService;
import com.jinxin.platform.cdsideparkmgr.utils.IdWorker;
import com.jinxin.platform.cdsideparkmgr.utils.http.HttpClient;
import com.jinxin.platform.cdsideparkmgr.utils.http.HttpMethod;
import com.jinxin.platform.cdsideparkmgr.utils.http.HttpRequest;
import com.jinxin.platform.cdsideparkmgr.utils.http.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author 那个男人
 * @Date 2022/9/19 17:41
 * @Version 1.0
 */
@Service
@Slf4j
public class CdsideparkmageDeviceServiceImpl implements ICdsideparkmageDeviceService {
    @Autowired
    private CdsideparkmgrDeviceOnlineMapper deviceOnlineMapper;
    @Resource
    private IdWorker idWorker;
    @Autowired
    private CdsideparkmgrProjectCameraMapper projectCameraMapper;
    @Autowired
    private CdsideparkmgrProjectMapper projectMapper;
    @Value("${cdsideparkmgr.zenith.snapPath}")
    private String snapPath;
    @Value("${cdsideparkmgr.zenith.videoPath}")
    private String videoPath;
    @Value("${cdsideparkmgr.zenith.mjpegVideoPath}")
    private String mjpegVideoPath;
    @Value("${cdsideparkmgr.zenith.devicePath}")
    private String devicePath;
    @Value("${cdsideparkmgr.zenith.berthPath}")
    private String berthPath;

    @Override
    public JsonResult heartbeatPush(Map<String, Object> param) {
        updateDeviceStatus(param);
        return null;
    }

    private void updateDeviceStatus(Map<String, Object> param) {
        String devName = (String) param.get("dev_name");
        String ip = (String) param.get("ip");
        String product = (String) param.get("product");
        String sn = (String) param.get("sn");
        if(!StringUtils.isEmpty(sn)){//
            CdparkmanagerProjectCamera projectCamera = queryProjectCamera(sn);
            if(projectCamera != null){
                HashMap param2 = new HashMap();
                param2.put("deviceMac", sn);
                CdyardmgrDeviceOnline deviceOnline = deviceOnlineMapper.selectOne(param2);
                String projectId = projectCamera.getProjectId();
                CdparkmanagerProject project = projectMapper.selectByKey(projectId);
                String projectName = project != null ? project.getProjectName() : "";
                Integer projectType = project != null ? project.getProjectType() : null;
                if (deviceOnline == null) {
                    deviceOnline = new CdyardmgrDeviceOnline();
                    deviceOnline.setId(idWorker.nextId() + "");
                    deviceOnline.setProjectId(projectId);
                    deviceOnline.setProjectName(projectName);
                    deviceOnline.setDeviceMac(sn);
                    deviceOnline.setDeviceName(projectCamera.getCameraName());
                    deviceOnline.setIsOnline(Constant.DEVICE_ON_LINE);
                    deviceOnline.setUpdateTime(new Date());
                    deviceOnline.setType(projectType);
                    deviceOnlineMapper.insert(deviceOnline);
                } else {
                    deviceOnline.setDeviceName(projectCamera.getCameraName());
                    deviceOnline.setIsOnline(Constant.DEVICE_ON_LINE);
                    deviceOnline.setUpdateTime(new Date());
                    deviceOnlineMapper.updateByKey(deviceOnline);
                }
            }
        }
        Map<String, List<String>> stateMap = (Map<String, List<String>>) param.get("state");
        List<String> parkList = stateMap.get("parking_number");
        parkList.forEach(item->{
            log.info(item);
        });
    }

    @Override
    public JsonResult resultPush(Map<String, Object> param) {
        log.info("中间件推送");
        for (Map.Entry<String,Object> entry : param.entrySet()){
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
        if(param.containsKey("alarm_info")){//识别结果推送
            Map<String, Integer> alarmStatusMap = (Map<String, Integer>) param.get("alarm_info");
            Integer alarmStatus = alarmStatusMap.get("alarm_status");
            System.out.println("alarmStatus: " + alarmStatus);
            Map<String, Object> productMap = (Map<String, Object>) param.get("product_h");
            Map<String,Object> carPosMap = (Map<String, Object>) productMap.get("car_pos");
            Map<String, Integer> locMap = (Map<String, Integer>) carPosMap.get("loc");
            Integer bottom = locMap.get("bottom");
            System.out.println("bottom: " + bottom);
            Integer left = locMap.get("left");
            System.out.println("left: " + left);
            Integer right = locMap.get("right");
            System.out.println("right: " + right);
            Integer top = locMap.get("top");
            System.out.println("top: " + top);
            Integer pos = (Integer) carPosMap.get("pos");
            System.out.println("pos: " + pos);
            Map<String,Object> parkingMap = (Map<String, Object>) productMap.get("parking");
            Map<String, Object> parklocMap = (Map<String, Object>) parkingMap.get("loc");
            Integer parkingState = (Integer) parkingMap.get("parking_state");
            System.out.println("parkingState: " + parkingState);
            Integer zoneId = (Integer) parkingMap.get("zone_id");
            String zoneName = (String) parkingMap.get("zone_name");
            List<Map<String,Integer>> pointList = (List<Map<String, Integer>>) parklocMap.get("point");
            pointList.forEach(pointMap->{
                Integer x = pointMap.get("x");
                Integer y = pointMap.get("y");
            });
            Map<String,Object> plateMap = (Map<String, Object>) productMap.get("plate");
            Integer color = (Integer) plateMap.get("color");
            Integer confidence = (Integer) plateMap.get("confidence");
            Map<String, Integer> platelocMap = (Map<String, Integer>) plateMap.get("loc");
            Integer platelocbottom = platelocMap.get("bottom");
            Integer platelocleft = platelocMap.get("left");
            Integer platelocright = platelocMap.get("right");
            Integer plateloctop = platelocMap.get("top");

            String plate = (String) plateMap.get("plate");
            Integer type = (Integer) plateMap.get("type");
            Map<String,Object> recoMap = (Map<String, Object>) productMap.get("reco");
            Integer groupId = (Integer) recoMap.get("group_id");
            Integer recoFlag = (Integer) recoMap.get("reco_flag");
            String recoTime = (String) recoMap.get("reco_time");
            Integer time_s = (Integer) param.get("time_s");
            if(param.containsKey("bg_img")){
                List<Map<String,Object>> imgList = (List<Map<String, Object>>) param.get("bg_img");
                imgList.forEach(imgMap->{
                    String base64img = (String) imgMap.get("image");
                    Integer imageLength = (Integer) imgMap.get("image_length");
                    Integer imageSendFlag = (Integer) imgMap.get("image_send_flag");
                    Integer imageKey = (Integer) imgMap.get("key");
                    String imagePath = (String) imgMap.get("path");
                    Map<String,Integer> resolutionMap = (Map<String, Integer>) imgMap.get("resolution");
                    Integer height = resolutionMap.get("height");
                    Integer width = resolutionMap.get("width");
                });
            }
        }else {//事件推送
            Map<String, Object> zoneMap = (Map<String, Object>) param.get("zone");
            Integer zoneId = (Integer) zoneMap.get("id");
            Map<String, Object> zonelocMap = (Map<String, Object>) zoneMap.get("loc");
            List<Map<String,Integer>> pointList = (List<Map<String, Integer>>) zonelocMap.get("point");
            pointList.forEach(pointMap->{
                Integer x = pointMap.get("x");
                Integer y = pointMap.get("y");
            });
            Integer pointNum = (Integer) zonelocMap.get("point_num");
            Map<String,Integer> sizeFactorMap = (Map<String, Integer>) zonelocMap.get("reco_zone_size_factor");
            Integer factorH = sizeFactorMap.get("factor_h");
            Integer factorW = sizeFactorMap.get("factor_w");
            String zoneName = (String) zoneMap.get("name");
            List<Map<String,Object>> productList = (List<Map<String, Object>>) param.get("product_h");
            Map<String, Object> productMap = productList.get(0);
            Map<String,Object> carPosMap = (Map<String, Object>) productMap.get("car_pos");
            Map<String, Integer> locMap = (Map<String, Integer>) carPosMap.get("loc");
            Integer bottom = locMap.get("bottom");
            System.out.println("bottom: " + bottom);
            Integer left = locMap.get("left");
            System.out.println("left: " + left);
            Integer right = locMap.get("right");
            System.out.println("right: " + right);
            Integer top = locMap.get("top");
            System.out.println("top: " + top);
            Integer pos = (Integer) carPosMap.get("pos");
            System.out.println("pos: " + pos);
            Map<String,Object> alarmInfoMap = (Map<String, Object>) productMap.get("alarm_info");
            Integer alarmStatus = (Integer) alarmInfoMap.get("alarm_status");
            System.out.println("alarmStatus: " + alarmStatus);

            if(productMap.containsKey("bg_img")){
                List<Map<String,Object>> imgList = (List<Map<String, Object>>) productMap.get("bg_img");
                imgList.forEach(imgMap->{
                    String base64img = (String) imgMap.get("image");
                    Integer imageLength = (Integer) imgMap.get("image_length");
                    Integer imageSendFlag = (Integer) imgMap.get("image_send_flag");
                    Integer imageKey = (Integer) imgMap.get("key");
                    String imagePath = (String) imgMap.get("path");
                    Map<String,Integer> resolutionMap = (Map<String, Integer>) imgMap.get("resolution");
                    Integer height = resolutionMap.get("height");
                    Integer width = resolutionMap.get("width");
                });
            }

            Integer parkingState = (Integer) productMap.get("parking_state");

            Map<String,Object> plateMap = (Map<String, Object>) productMap.get("plate");
            Integer color = (Integer) plateMap.get("color");
            Integer confidence = (Integer) plateMap.get("confidence");
            Map<String, Integer> platelocMap = (Map<String, Integer>) plateMap.get("loc");
            Integer platelocbottom = platelocMap.get("bottom");
            Integer platelocleft = platelocMap.get("left");
            Integer platelocright = platelocMap.get("right");
            Integer plateloctop = platelocMap.get("top");
            String plate = (String) plateMap.get("plate");
            Integer type = (Integer) plateMap.get("type");

            Integer time = (Integer) productMap.get("time_s");

        }
        Integer chnlid = (Integer) param.get("chnlid");
        System.out.println("chnlid: " + chnlid);
        Map<String, String> deviceInfoMap = (Map<String, String>) param.get("device_info");
        String devName = deviceInfoMap.get("dev_name");
        System.out.println("devName: " + devName);
        String ip = deviceInfoMap.get("ip");
        System.out.println("ip: " + ip);
        String sn = deviceInfoMap.get("sn");
        System.out.println("sn: " + sn);
        String protocol = (String) param.get("protocol");
        Integer recoId = (Integer) param.get("reco_id");
        Integer serial = (Integer) param.get("serial");

        Map<String, Object> triggerExtendMap = (Map<String, Object>) param.get("trigger_extend");
        String userData = (String) triggerExtendMap.get("user_data");
        Integer tzoneId = (Integer) triggerExtendMap.get("zone_id");
        Integer triggerType = (Integer) param.get("trigger_type");
        System.out.println("triggerType: " + triggerType);
        return null;
    }

    @Override
    public JsonResult heartbeatPushForHeight(Map<String, Object> param) {
        updateDeviceStatus(param);
        return null;
    }

    @Override
    public JsonResult resultPushForHeight(Map<String, Object> param) {
        if(param.containsKey("alarm_info")){//识别结果推送
            Map<String, Integer> alarmStatusMap = (Map<String, Integer>) param.get("alarm_info");
            Integer alarmStatus = alarmStatusMap.get("alarm_status");
            System.out.println("alarmStatus: " + alarmStatus);
            Map<String, Object> productMap = (Map<String, Object>) param.get("product_h");
            Map<String,Object> carPosMap = (Map<String, Object>) productMap.get("car_pos");
            Integer pos = (Integer) carPosMap.get("pos");
            System.out.println("pos: " + pos);
            Map<String,Object> parkingMap = (Map<String, Object>) productMap.get("parking");
            Map<String, Object> parklocMap = (Map<String, Object>) parkingMap.get("loc");
            Integer parkingState = (Integer) parkingMap.get("parking_state");
            System.out.println("parkingState: " + parkingState);
            Integer zoneId = (Integer) parkingMap.get("zone_id");
            String zoneName = (String) parkingMap.get("zone_name");
            List<Map<String,Integer>> pointList = (List<Map<String, Integer>>) parklocMap.get("point");
            pointList.forEach(pointMap->{
                Integer x = pointMap.get("x");
                Integer y = pointMap.get("y");
            });
            Map<String,Object> plateMap = (Map<String, Object>) productMap.get("plate");
            Integer color = (Integer) plateMap.get("color");
            Integer confidence = (Integer) plateMap.get("confidence");
            Map<String, Integer> platelocMap = (Map<String, Integer>) plateMap.get("loc");
            Integer platelocbottom = platelocMap.get("bottom");
            Integer platelocleft = platelocMap.get("left");
            Integer platelocright = platelocMap.get("right");
            Integer plateloctop = platelocMap.get("top");

            String plate = (String) plateMap.get("plate");
            Integer type = (Integer) plateMap.get("type");
            Map<String,Object> recoMap = (Map<String, Object>) productMap.get("reco");
            Integer groupId = (Integer) recoMap.get("group_id");
            Integer recoFlag = (Integer) recoMap.get("reco_flag");
            String recoTime = (String) recoMap.get("reco_time");
            Integer time_s = (Integer) param.get("time_s");
            if(param.containsKey("bg_img")){
                List<Map<String,Object>> imgList = (List<Map<String, Object>>) param.get("bg_img");
                imgList.forEach(imgMap->{
                    String base64img = (String) imgMap.get("image");
                    Integer imageLength = (Integer) imgMap.get("image_length");
                    Integer imageSendFlag = (Integer) imgMap.get("image_send_flag");
                    Integer imageKey = (Integer) imgMap.get("key");
                    String imagePath = (String) imgMap.get("path");
                    Map<String,Integer> resolutionMap = (Map<String, Integer>) imgMap.get("resolution");
                    Integer height = resolutionMap.get("height");
                    Integer width = resolutionMap.get("width");
                });
            }
        }else {//事件推送
            Map<String, Object> zoneMap = (Map<String, Object>) param.get("zone");
            Integer zoneId = (Integer) zoneMap.get("id");
            Map<String, Object> zonelocMap = (Map<String, Object>) zoneMap.get("loc");
            List<Map<String,Integer>> pointList = (List<Map<String, Integer>>) zonelocMap.get("point");
            pointList.forEach(pointMap->{
                Integer x = pointMap.get("x");
                Integer y = pointMap.get("y");
            });
            Integer pointNum = (Integer) zonelocMap.get("point_num");
            Map<String,Integer> sizeFactorMap = (Map<String, Integer>) zonelocMap.get("reco_zone_size_factor");
            Integer factorH = sizeFactorMap.get("factor_h");
            Integer factorW = sizeFactorMap.get("factor_w");
            String zoneName = (String) zoneMap.get("name");
            List<Map<String,Object>> productList = (List<Map<String, Object>>) param.get("product_h");
            Map<String, Object> productMap = productList.get(0);
            Map<String,Object> carPosMap = (Map<String, Object>) productMap.get("car_pos");
            Map<String, Integer> locMap = (Map<String, Integer>) carPosMap.get("loc");
            if(locMap != null){
                Integer bottom = locMap.get("bottom");
                System.out.println("bottom: " + bottom);
                Integer left = locMap.get("left");
                System.out.println("left: " + left);
                Integer right = locMap.get("right");
                System.out.println("right: " + right);
                Integer top = locMap.get("top");
                System.out.println("top: " + top);
            }
            Integer pos = (Integer) carPosMap.get("pos");
            System.out.println("pos: " + pos);
            Map<String,Object> alarmInfoMap = (Map<String, Object>) productMap.get("alarm_info");
            Integer alarmStatus = (Integer) alarmInfoMap.get("alarm_status");
            System.out.println("alarmStatus: " + alarmStatus);

            if(productMap.containsKey("bg_img")){
                List<Map<String,Object>> imgList = (List<Map<String, Object>>) productMap.get("bg_img");
                imgList.forEach(imgMap->{
                    String base64img = (String) imgMap.get("image");
                    Integer imageLength = (Integer) imgMap.get("image_length");
                    Integer imageSendFlag = (Integer) imgMap.get("image_send_flag");
                    Integer imageKey = (Integer) imgMap.get("key");
                    String imagePath = (String) imgMap.get("path");
                    Map<String,Integer> resolutionMap = (Map<String, Integer>) imgMap.get("resolution");
                    Integer height = resolutionMap.get("height");
                    Integer width = resolutionMap.get("width");
                });
            }

            Integer parkingState = (Integer) productMap.get("parking_state");

            Map<String,Object> plateMap = (Map<String, Object>) productMap.get("plate");
            Integer color = (Integer) plateMap.get("color");
            Integer confidence = (Integer) plateMap.get("confidence");
            Map<String, Integer> platelocMap = (Map<String, Integer>) plateMap.get("loc");
            Integer platelocbottom = platelocMap.get("bottom");
            Integer platelocleft = platelocMap.get("left");
            Integer platelocright = platelocMap.get("right");
            Integer plateloctop = platelocMap.get("top");
            String plate = (String) plateMap.get("plate");
            Integer type = (Integer) plateMap.get("type");

            Integer time = (Integer) productMap.get("time_s");

        }
        Integer chnlid = (Integer) param.get("chnlid");
        System.out.println("chnlid: " + chnlid);
        Map<String, String> deviceInfoMap = (Map<String, String>) param.get("device_info");
        String devName = deviceInfoMap.get("dev_name");
        System.out.println("devName: " + devName);
        String ip = deviceInfoMap.get("ip");
        System.out.println("ip: " + ip);
        String sn = deviceInfoMap.get("sn");
        System.out.println("sn: " + sn);
        String protocol = (String) param.get("protocol");
        Integer recoId = (Integer) param.get("reco_id");
        Integer serial = (Integer) param.get("serial");

        Map<String, Object> triggerExtendMap = (Map<String, Object>) param.get("trigger_extend");
        String userData = (String) triggerExtendMap.get("user_data");
        Integer tzoneId = (Integer) triggerExtendMap.get("zone_id");
        Integer triggerType = (Integer) param.get("trigger_type");
        System.out.println("triggerType: " + triggerType);
        return null;
    }

    @Override
    public JsonResult snap(String sn) {
        CdparkmanagerProjectCamera projectCamera = queryProjectCamera(sn);
        if(projectCamera == null){
            return JsonResult.error("该设备未绑定到项目中");
        }
        HttpRequest request = new HttpRequest(HttpMethod.POST, this.snapPath);
        String[] arr = new String[]{sn};
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sns",arr);
        request.setJsonBody(jsonObject.toJSONString());
//        String baseUrl = "http://www.parkways.cn";
//        String accesskeyId = "GFf7j3gF22w15LSD9keInE1QyHAlrHSr";
//        String accesskeySecret = "HyAwV5PFYs8mdJD5o49GQV3Izn3hssJX";
        CdparkmanagerProject project = projectMapper.selectByKey(projectCamera.getProjectId());
        if(project == null){return JsonResult.error("项目不存在");}
        String baseUrl = project.getCameraUrl();
        String accesskeyId = project.getCameraAk();
        String accesskeySecret = project.getCameraSk();
        HttpClient client = new HttpClient(baseUrl, accesskeyId, accesskeySecret);
        HttpResponse response = client.doRequest(request);
        int status = response.getStatus();
        String bodyStr = response.getBodyStr();
        if (status == Constant.HTTP_STATUS_OK) {
            JSONObject jsonObj = JSONObject.parseObject(bodyStr);
            return JsonResult.ok(jsonObj.toJSONString());
        }
        return JsonResult.error(bodyStr);
    }

    private CdparkmanagerProjectCamera queryProjectCamera(String sn) {
        HashMap param1 = new HashMap();
        param1.put("cameraMac", sn);
        return projectCameraMapper.selectOne(param1);
    }

    @Override
    public JsonResult queryVideoUrl(String sn) {
        return queryUrl(sn,this.videoPath);
    }

    private JsonResult queryUrl(String sn,String path){
        CdparkmanagerProjectCamera projectCamera = queryProjectCamera(sn);
        if(projectCamera == null){
            return JsonResult.error("该设备未绑定到项目中");
        }
        CdparkmanagerProject project = projectMapper.selectByKey(projectCamera.getProjectId());
        if(project == null){return JsonResult.error("项目不存在");}
        String baseUrl = project.getCameraUrl();
        String accesskeyId = project.getCameraAk();
        String accesskeySecret = project.getCameraSk();
        return getVideoUrl(sn,path,baseUrl,accesskeyId,accesskeySecret);
    }

    private JsonResult getVideoUrl(String sn,String path,String baseUrl,String ak,String sk){
        HttpRequest request = new HttpRequest(HttpMethod.POST, path);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sn",sn);
        request.setJsonBody(jsonObject.toJSONString());
        HttpClient client = new HttpClient(baseUrl, ak, sk);
        HttpResponse response = client.doRequest(request);
        int status = response.getStatus();
        String bodyStr = response.getBodyStr();
        if (status == Constant.HTTP_STATUS_OK) {
            ResponseData responseData = JSONObject.parseObject(bodyStr, ResponseData.class);
            return JsonResult.ok(responseData);
        }
        return JsonResult.error(bodyStr);
    }

    @Override
    public JsonResult queryMjpegVideoUrl(String sn) {
        return queryUrl(sn,this.mjpegVideoPath);
    }

    @Override
    public JsonResult pushResult(PushInfo pushInfo) {
        if(pushInfo == null){return JsonResult.error("推送结果为空");}
        Integer evt = pushInfo.getEvt();
        String deviceSn = pushInfo.getDeviceSn();
        if(StringUtils.isEmpty(evt) || StringUtils.isEmpty(deviceSn)){return JsonResult.error("结果类型或设备序列号参数缺失");}
        if(evt.equals(ResultType.CODE_16.getCode())){//报警事件需要推送给云坐席

        }
        return null;
    }

    @Override
    public JsonResult listVideoUrlList(String projectId,String cameraName,String cameraArea) {
        HashMap param = new HashMap();
        param.put("projectId",projectId);
        param.put("cameraName",cameraName);
        param.put("cameraArea",cameraArea);
        List<CdparkmanagerProjectCamera> list = projectCameraMapper.list(param);
        if(list.size() == 0){return JsonResult.error("该项目未绑定相机");}
        CdparkmanagerProject project = projectMapper.selectByKey(projectId);
        if(project == null){return JsonResult.error("项目不存在");}
        String baseUrl = project.getCameraUrl();
        String accesskeyId = project.getCameraAk();
        String accesskeySecret = project.getCameraSk();
        List<JSONObject> urlList = new ArrayList<>();
        for(CdparkmanagerProjectCamera item : list){
            String sn = item.getCameraMac();
            if(StringUtils.isEmpty(sn)){
                continue;
            }
//            String baseUrl = "http://www.parkways.cn";
//            String accesskeyId = "GFf7j3gF22w15LSD9keInE1QyHAlrHSr";
//            String accesskeySecret = "HyAwV5PFYs8mdJD5o49GQV3Izn3hssJX";
            JsonResult jsonResult = getVideoUrl(sn, this.videoPath, baseUrl, accesskeyId, accesskeySecret);
            if(jsonResult.getSuccess()){
                ResponseData responseData = (ResponseData) jsonResult.getData();
                if(Constant.ERROR_CODE_NORMAL.equals(responseData.getErrorcode())){
                    JSONObject jsonObject = JSONObject.parseObject(responseData.getData().toString());
                    JSONObject snUrlJson = new JSONObject();
                    snUrlJson.put("name",item.getCameraName());
                    snUrlJson.put("sn",sn);
                    snUrlJson.put("url",jsonObject.getString("url"));
                    urlList.add(snUrlJson);
                }
            }
        }
        return JsonResult.ok(urlList);
    }

    @Override
    public JsonResult queryStallStatus(String sn,Integer page,Integer limit) {
        CdparkmanagerProjectCamera projectCamera = queryProjectCamera(sn);
        if(projectCamera == null){
            return JsonResult.error("该设备未绑定到项目中");
        }
        CdparkmanagerProject project = projectMapper.selectByKey(projectCamera.getProjectId());
        if(project == null){return JsonResult.error("项目不存在");}
        String baseUrl = project.getCameraUrl();
        String accesskeyId = project.getCameraAk();
        String accesskeySecret = project.getCameraSk();
        HttpRequest request = new HttpRequest(HttpMethod.POST, this.devicePath);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("deviceSn",sn);
        request.setJsonBody(jsonObject.toJSONString());
        HttpClient client = new HttpClient(baseUrl, accesskeyId, accesskeySecret);
        HttpResponse response = client.doRequest(request);
        int status = response.getStatus();
        String bodyStr = response.getBodyStr();
        if (status == Constant.HTTP_STATUS_OK) {
            ResponseData responseData = JSONObject.parseObject(bodyStr, ResponseData.class);
            if(Constant.ERROR_CODE_NORMAL.equals(responseData.getErrorcode())){
                JSONObject jsonObj = (JSONObject) responseData.getData();
                Integer parkingCode = jsonObj.getInteger("parkingCode");
                // 泊位查询(需要车场编号)
                HttpRequest request2 = new HttpRequest(HttpMethod.POST, this.berthPath);
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("parkingCode",parkingCode);
                if(page != null){
                    jsonObject2.put("page",page);
                }
                if(limit != null){
                    jsonObject2.put("size",limit);
                }
                request2.setJsonBody(jsonObject2.toJSONString());
                HttpResponse response2 = client.doRequest(request2);
                int status2 = response2.getStatus();
                String bodyStr2 = response2.getBodyStr();
                if (status2 == Constant.HTTP_STATUS_OK) {
                    ResponseData responseData2 = JSONObject.parseObject(bodyStr2, ResponseData.class);
                    return JsonResult.ok(responseData2.getData());
                }else {
                    return JsonResult.error(bodyStr2);
                }
            }
            return JsonResult.error(responseData.getMessage());
        }
        return JsonResult.error(bodyStr);
    }
}
